//
//  MatchViewController.m
//  Full_House
//
//  Created by 张忱 on 15-6-3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MatchViewController.h"
#import "Match.h"
#import "MatchCell.h"
#import "CaseViewController.h"
#import "MatchCell.h"
#import "DaLiCell.h"
@interface MatchViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *matchArr;
@property (nonatomic, retain) UITableView *mytableView;
@property (nonatomic, retain) UIScrollView *backScrollView;
@property (nonatomic, retain) UIButton *matchButton;
@property (nonatomic, retain) UIButton *danButton;
@property (nonatomic, retain) UICollectionView *myCollectionView;
@property (nonatomic, retain) NSMutableArray *daLiArr;
@end

@implementation MatchViewController

- (void)dealloc{
    [_matchArr release];
    [_mytableView release];
    [_backScrollView release];
    [_matchButton release];
    [_danButton release];
    [_myCollectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dealDate];
    [self setupSubViews];
    
    

}

- (void)setupSubViews{
    
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"筛选" style:UIBarButtonSystemItemDone target:self action:@selector(tabBarAction:)];
    
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 40)];
    //topView.backgroundColor = [UIColor redColor];
    [self.view addSubview:topView];
    [topView release];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2, 0, 2, 40)];
    imageView.image = [UIImage imageNamed:@"08514947CEE15A000E0DE5F43058EFD1.png"];
    [topView addSubview:imageView];
    [imageView release];
    
    self.matchButton = [UIButton buttonWithTitle:@"搭配" frame:CGRectMake(0, 0, 375 / 2 - 5, 40) target:self action:@selector(matchButtonAction:)];
    
    
    [_matchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _matchButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [topView addSubview:_matchButton];
    
    self.danButton = [UIButton buttonWithTitle:@"单例" frame:CGRectMake(375/ 2 + 10, 0, 375 / 2 - 5, 40) target:self action:@selector(danButtonAction:)];
    [_danButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _danButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [topView addSubview:_danButton];
    
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 104, self.view.bounds.size.width, self.view.bounds.size.height - 40)];
    _backScrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, 0);
    _backScrollView.contentOffset = CGPointMake(0, 0);
    _backScrollView.pagingEnabled = YES;
    _backScrollView.bounces = YES;
    _backScrollView.showsHorizontalScrollIndicator = YES;
    _backScrollView.delegate = self;
    _backScrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_backScrollView];
    
    self.mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.backScrollView.bounds.size.height) style:UITableViewStylePlain];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    [_backScrollView addSubview:_mytableView];
    [_mytableView release];
    [self.mytableView registerClass:[MatchCell class] forCellReuseIdentifier:@"matchReuse"];
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width / 2 - 20 - 5, 200);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumLineSpacing = 5;
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.backScrollView.bounds.size.height) collectionViewLayout:flowLayout];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    [_backScrollView addSubview:_myCollectionView];
    [_myCollectionView release];
    [_myCollectionView registerClass:[DaLiCell class] forCellWithReuseIdentifier:@"daLiReuse"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.matchArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 400;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"matchReuse"];
    Match *match = [self.matchArr objectAtIndex:indexPath.row];
    cell.match = match;
    
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.daLiArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DaLiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"daLiReuse" forIndexPath:indexPath];
    DanLi *danLi = [self.daLiArr objectAtIndex:indexPath.item];
    cell.daLi = danLi;
    return cell;
    
}


- (void)matchButtonAction:(UIButton *)button{
    [self.matchButton setTintColor:[UIColor whiteColor]];
    [self.danButton setTintColor:[UIColor blackColor]];
       self.backScrollView.contentOffset = CGPointMake(0, 0);
    
}
- (void)danButtonAction:(UIButton *)button{
    [self.danButton setTintColor:[UIColor whiteColor]];
    [self.matchButton setTintColor:[UIColor blackColor]];
    self.backScrollView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
}



- (void)dealDate{
    NSString *str = @"http://api.guju.com.cn/v2/photo/flownew/?start=0&count=5&user=null&cate=0&style=0&color=0&";
    NSString *dealStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:dealStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *photosArr = [responseObject objectForKey:@"photos"];
        self.matchArr = [Match matchArrWithDic:photosArr];
        [self.mytableView reloadData];
        
       
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    NSString *daLiStr = @"http://api.guju.com.cn/v2/product/productFlowNew/?start=8&count=8&user=null&cate=0&style=0&color=0&";
    NSString *dealDaLiStr = [daLiStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *daliManager = [AFHTTPRequestOperationManager manager];
    daliManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [daliManager GET:dealDaLiStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *daLiArr = [responseObject objectForKey:@"products"];
        self.daLiArr = [DanLi danLiArrWithDic:daLiArr];
        [self.myCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
